package com.fancymusic.baidu;

import com.fancymusic.baidu.pojo.Face_list;
import com.fancymusic.baidu.pojo.JsonRootBean;
import com.fancymusic.utils.GsonUtils;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class Draw {
    public static void main(String[] args) throws IOException {
        String url = "D:\\Documents\\3.jpg";
        String url01 = "D:\\teset.jpg";
        // 调用百度api获取参数
        String resultjson = FaceDetect.faceDetect(url);
        System.out.println(resultjson);
        JsonRootBean rootBean = GsonUtils.fromJson(resultjson,JsonRootBean.class);
        Face_list list = rootBean.result.face_list[0];

        int top = (int)list.getLocation().getTop();
        int left = (int)list.getLocation().getLeft();
        int width = (int)list.getLocation().getWidth();
        int height = (int)list.getLocation().getHeight();

        //=======================
        InputStream in = new FileInputStream(url);//图片路径
        BufferedImage image = ImageIO.read(new File(url));
        Graphics g = image.getGraphics();
        g.setColor(Color.green);//画笔颜色
        g.setFont(new Font("隶书",Font.BOLD,15));
        g.drawString("年龄"+list.getAge(),left,top);
        g.drawRect(left, top, width, height);//矩形框(原点x坐标，原点y坐标，矩形的长，矩形的宽)

        //g.dispose();
        FileOutputStream out = new FileOutputStream(url01);//输出图片的地址
        ImageIO.write(image, "jpeg", out);

        // 创建窗口并显示

        JFrame f = new JFrame("FaceDetect");
        f.setSize(image.getWidth(),image.getHeight());
        f.setLocation(0,0);
        f.setLayout(null);// 默认
        JLabel label = new JLabel();
        ImageIcon i = new ImageIcon(url01);
        label.setIcon(i);
        label.setBounds(0,0,i.getIconWidth(), i.getIconHeight());
        f.add(label);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

    }
}
